package com.peakmain.wanandroid.projectmodule;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.peakmain.wanandroid.netmodule.bean.project.ProjectTreeBean;
import com.peakmain.wanandroid.projectmodule.base.BaseProjectFragment;
import com.peakmain.wanandroid.projectmodule.contract.ProjectTreeContract;
import com.peakmain.wanandroid.projectmodule.fragment.ProjectListFragment;
import com.peakmain.wanandroid.projectmodule.presenter.ProjectTreePresenter;
import com.peakmain.wanandroid.projectmodule.view.tablelayout.TabLayout;

import java.util.List;

import butterknife.BindView;

public class ProjectFragment extends BaseProjectFragment<ProjectTreePresenter> implements ProjectTreeContract.View {

    @BindView(R2.id.view_pager)
    ViewPager mViewPager;
    @BindView(R2.id.tab_layout)
    TabLayout mTabLayout;
    private static final String TAG = ProjectFragment.class.getSimpleName();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_project;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initView(View view) {
        mPresenter.getProjectTree();
    }

    /**
     * 初始化ViewPager
     */

    private void initViewPager(final List<ProjectTreeBean> bean) {
        mViewPager.setAdapter(new FragmentPagerAdapter(this.getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                return ProjectListFragment.newInstance(bean.get(position).getId());
            }

            @Override
            public int getCount() {
                return bean.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
            }

        });

    }


    @Override
    public void showProjectTreeSuccess(List<ProjectTreeBean> bean) {
        initViewPager(bean);
        mTabLayout.initIndicator(bean, mViewPager);
        mTabLayout.setOnTabItemClickListener(new TabLayout.OnTabItemClickListener() {
            @Override
            public void onTabItem(int position) {
                mViewPager.setCurrentItem(position);
            }
        });
    }

}
